package com.jme3.app;

import android.app.Activity;
import android.os.Bundle;
import android.content.pm.ActivityInfo;
import android.opengl.GLSurfaceView;
import android.view.Window;
import android.view.WindowManager;
import com.jme3.system.AppSettings;
import com.jme3.system.JmeSystem;
import com.jme3.system.android.OGLESContext;
import com.jme3.app.android.AndroidApplication;
import com.jme3.input.android.AndroidInput;
import java.util.logging.Logger;
import java.util.logging.Handler;
import java.util.logging.SimpleFormatter;


public class MainActivity extends Activity
{
    private final static Logger logger = Logger.getLogger(MainActivity.class.getName());
    
    private OGLESContext ctx;
    private GLSurfaceView view;
    
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {           
        super.onCreate(savedInstanceState);
        
        Logger log = logger;
        boolean bIsLogFormatSet = false;
        do
        {
            if (log.getHandlers().length == 0)
            {
                log = logger.getParent();
                if (log != null)
                    for (Handler h : log.getHandlers())
                    {
                        h.setFormatter(new SimpleFormatter());
                        bIsLogFormatSet = true;
                    }
            }
        } while (log != null && !bIsLogFormatSet);
        
        
        JmeSystem.setResources(getResources());
    
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
        WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        getWindow().makeActive();
        
        
        AppSettings settings = new AppSettings(true);                            
        AndroidInput input = new AndroidInput(this);
        
        // User App created here
        AndroidApplication app = new MyApp(this, input);
    
        
        //logger.info("setting settings ...");
        //app.setSettings(settings);
        //logger.info("setting settings ... done.");
    
        logger.info("starting app ...");
        app.start();
        logger.info("starting app ... done.");
    
        logger.info("creating context ...");
        ctx = (OGLESContext) app.getContext();
        logger.info("creating context ... done.");
    
        logger.info("creating view ...");
        view = ctx.createView(input);
        logger.info("creating view ... done.");
    
        logger.info("setting content view ...");
        setContentView(view);
        logger.info("setting content done ...");
    }
    
    @Override 
    protected void onResume() 
    {
        super.onResume();
        view.onResume();
    }
    
    @Override
    protected void onPause() 
    {
        super.onPause();
        view.onPause();
    }
}